/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//Speeds player up
//Calculates line intersections to display rotational graphics and to determine
//when the object has touched the player.

package chromafinal;

/**
 *
 * @author Benjamin
 */

import java.awt.Color;
import java.awt.Graphics;

public class Boost extends LevelObject{

    int testing;

    double [][] coords = new double[4][2];
    double [] fmod = new double[2];
    int [][][] pcoords;
    int [][] pycoords;
    int wblocks, hblocks;
    int force;
    int [] boxlim;
    int bottomcoord; //Index of bottommost coordinate
    boolean ortho;
    boolean mortho;
    int [] crdm;

    final double blocksize = 30.0;



    Boost(int[] crd, int force)
    {
        new Boost(crd, false, force);
    }

    Boost(int[] crd, boolean ortho, int force)
    {

        testing = 0;
        otype = BOOST;

        crdm = crd;

        this.force = 100;
        mortho = ortho;
        this.ortho = ortho;

        if (!ortho && ((crd[0] == crd[2]) || (crd[1] == crd[3])))
        {
            if (crd[0] == crd[2])
            {
                crdm[2] = crdm[4];
            }
            if(crd[1] == crd[3])
            {
                crdm[3] = crdm[5];
            }
            this.ortho = true;
        }



        if (ortho)
        {
            coords[0][0] = crd[0];
            coords[0][1] = crd[1];
            coords[2][0] = crdm[2];
            coords[2][1] = crdm[3];
            if (coords[0][0] < coords[2][0])
                if (coords[0][1] < coords[2][1])
                    fmod = new double [] {0.0,1.0};
                else
                    fmod = new double [] {1.0,0.0};
            else
                if (coords[0][1] < coords[2][1])
                    fmod = new double [] {-1.0, 0.0};
                else
                    fmod = new double [] {0.0,-1.0};
            coords[1][0] = coords[0][0];
            coords[1][1] = coords[2][1];
            coords[3][0] = coords[2][0];
            coords[3][1] = coords[0][1];
        }
        else
        {
            for (int i = 0; i <4; i++)
                coords[i/2][i%2] = crd[i];

            int[] tcors = new int[] {crd[4], crd[5]};
            double x1 = coords[0][0];
            double x2 = coords[1][0];
            double x3 = tcors[0];
            double y1 = coords[0][1];
            double y2 = coords[1][1];
            double y3 = tcors[1];

            double xd = x2-x1;
            double yd = y2-y1;

            double xtk2 = (x2 * (xd/yd) + x3*(yd/xd) + y2 - y3)/(yd/xd + xd/yd);
            double xtc = xtk2;
            double ytc = (xtk2-x2) * (-1.0 * (xd/yd)) + y2;

            coords[3][0] = (xtc-xd);
            coords[3][1] = (ytc-yd);
            coords[2][0] = xtc;
            coords[2][1] = ytc;

            double xfc = xtc-x2;
            double yfc = ytc-y2;
            double dmult = Math.pow(xfc*xfc + yfc*yfc, 0.5);
            fmod[0] = xfc/dmult;
            fmod[1] = yfc/dmult;

        }

        int minxc = (int)Math.min(Math.min(coords[0][0],coords[1][0]),Math.min(coords[2][0],coords[3][0]));
        int maxxc = (int)Math.max(Math.max(coords[0][0],coords[1][0]),Math.max(coords[2][0],coords[3][0]));

        int minyc = (int)Math.min(Math.min(coords[0][1],coords[1][1]),Math.min(coords[2][1],coords[3][1]));
        int maxyc = (int)Math.max(Math.max(coords[0][1],coords[1][1]),Math.max(coords[2][1],coords[3][1]));

        leftx = minxc;
        rightx = maxxc;

        boxlim = new int[] {minxc,minyc,maxxc,maxyc};

            double x1 = coords[0][0];
            double x2 = coords[1][0];

            double y1 = coords[0][1];
            double y2 = coords[1][1];

            double xd = x2-x1;
            double yd = y2-y1;

            
            double width = Math.pow(xd*xd + yd*yd,0.5);
            double height = Math.pow(Math.pow(coords[2][0]-x2,2)+
                    Math.pow(coords[2][1]-y2,2),0.5);

            wblocks = Math.max((int)(width/blocksize + 0.5),1);
            hblocks = Math.max((int)(height/blocksize + 0.5),1);


            pcoords = new int[2][wblocks*hblocks*2][7];

            double[] wmod = rotConv(0.0,0.0,1,0);
            double[] hmod = rotConv(0.0,0.0,0,1);


            double[][] greenbase = oneArrow();
            double[][] redbase = new double[7][2];

            for (int i = 0; i < 7; i++)
                for (int j = 0; j <= 1; j++)
                    redbase[i][j] = -greenbase[i][j] + wmod[j] + hmod[j];
                    

            

            double [] xmod = new double[2];
            


            for (int i = 0; i < hblocks; i++)
                for (int j = 0; j < wblocks; j++)
                {
                    for (int k = 0; k < 7; k++)
                    {
                        xmod = rotConv(0.0,0.0,j,i);
                        pcoords[0][i*wblocks+j][k] = (int)(xmod[0] + greenbase[k][0] + coords[0][0]);
                        pcoords[1][i*wblocks+j][k] = (int)(xmod[1] + greenbase[k][1] + coords[0][1]);
                        pcoords[0][i*wblocks+j+wblocks*hblocks][k] = (int) (xmod[0] + redbase[k][0] + coords[0][0]);
                        pcoords[1][i*wblocks+j+wblocks*hblocks][k] = (int) (xmod[1] + redbase[k][1] + coords[0][1]);
                    }
                }
        }
    
    public double[][] oneArrow()
    {
        double[] dcrds;
        double[][] icrds = new double[7][2];

        icrds[0] = rotConv(0.115,0.15);
        icrds[1] = rotConv(0.77/2.0,0.15);
        icrds[2] = rotConv(0.77/2.0,1.25/2.0);
        icrds[3] = rotConv(0.88/2.0,1.25/2.0);
        icrds[4] = rotConv(0.25,1.7/2.0);
        icrds[5] = rotConv(0.12/2.0,1.25/2.0);
        icrds[6] = rotConv(0.115,1.25/2);

        return icrds;
    }



    public double [] rotConv(double xr, double yr, int w, int h)
    {
        double[] convCoords = new double[2];
        convCoords[0] = (xr + w)/(1.0 * wblocks)*(coords[1][0]-coords[0][0]);
        convCoords[1] = (xr + w)/(1.0 * wblocks)*(coords[1][1]-coords[0][1]);
        
        convCoords[0]+= (yr + h)/(1.0*hblocks)*(coords[3][0]-coords[0][0]);
        convCoords[1]+= (yr + h)/(1.0*hblocks)*(coords[3][1]-coords[0][1]);


        return new double[] {(convCoords[0]),(convCoords[1])};
    }

    public double [] rotConv(double xr, double yr)
    {
        return rotConv(xr,yr,0,0);
    }

    public void pushPlayer(Player p)
    {
        int pmass = p.mass, mod = p.cnum;

        int [] rcrds = p.getRelCoords();

        if (rcrds[0] > boxlim[0] && rcrds[0] < boxlim[2] && rcrds[1] > boxlim[1] && rcrds[1] < boxlim[3])
        {
            if (!ortho)
            {
                double x1 = coords[0][0];
                double y1 = coords[0][1];
                double x2 = coords[1][0];
                double y2 = coords[1][1];
                double x3 = coords[2][0];
                double y3 = coords[2][1];
                double slp = (y2-y1)/(x2-x1);
                double ycl1 = (rcrds[0]-x1)*slp + y1;
                double ycl2 = (rcrds[0]-x3)*slp + y3;
                if (!(rcrds[1] > Math.min(ycl1,ycl2) && rcrds[1] < Math.max(ycl1,ycl2)))
                    return;

                slp = -1.0 * (x2-x1)/(y2-y1);
                ycl1 = (rcrds[0]-x2)*slp + y2;
                ycl2 = (rcrds[0]-x1)*slp + y1;

                if (!(rcrds[1] > Math.min(ycl1,ycl2) && rcrds[1] < Math.max(ycl1,ycl2)))
                    return;

            }

            double spd = Math.pow(p.xv*p.xv + p.yv*p.yv,0.5);

            p.xv+= force * fmod[0]*mod * -0.6;
            p.yv+= force * fmod[1]*mod * -0.6;

            double spd2 = Math.pow(p.xv*p.xv + p.yv*p.yv,0.5);

            if (spd > 500.0 && spd2 > spd)
            {
                p.xv = (spd/spd2) * p.xv;
                p.yv = (spd/spd2) * p.yv;
            }
        }

    }


    public void draw(Graphics g)
	{

            int x = Game.screenloc;
                g.setColor(Color.CYAN);

            int crs[][] =  new int[2][4];

            for (int i = 0; i < 4; i++)
            {
                crs[0][i] = (int)coords[i][0] - x;
                crs[1][i] = (int)coords[i][1];
            }
            g.fillPolygon(crs[0],crs[1],4);


            int [][][] adjcoords = new int [2][wblocks*hblocks*2][7];

            for (int i = 0; i < hblocks*wblocks*2; i++)
                for (int k = 0; k < 7; k++)
                {
                    adjcoords[0][i][k] = pcoords[0][i][k]-x;
                    adjcoords[1][i][k] = pcoords[1][i][k];
                }


            g.setColor(Color.green);
            for (int i = 0; i < hblocks*wblocks; i++)
            {
                g.fillPolygon(adjcoords[0][i],adjcoords[1][i],7);
            }

            g.setColor(Color.red);
            for (int i = 0; i < hblocks*wblocks; i++)
            {
                g.fillPolygon(adjcoords[0][i+hblocks*wblocks],adjcoords[1][i+hblocks*wblocks],7);
            }


	}


    public String makeText(){
            String oput = otype + "\t" + crdm[0] + "\t" + crdm[1]
                    + "\t" + crdm[2] + "\t" + crdm[3] + "\t" +
                    crdm[4] + "\t" + crdm[5] + "\t" + force + "\n";
            return oput;
	}
}
